package 测试百万数据的导出;

import com.itheima.pojo.User;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler {
    private User user = null;
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public void startRow(int rowIndex) { // 每一行开始，rowIndex代表每一个sheet的行索引
        if(rowIndex == 0) {
            user = null;
        }else {
            user = new User();
        }
    }

    @Override // 处理每一行的所有单元格
    public void cell(String cellName, String cellValue, XSSFComment xssfComment) {
        if(user != null) {
            String letter = cellName.substring(0,1); // 每个单元格名称的首字母 A B C
            switch (letter) {
                case "A":
                    user.setId(Long.parseLong(cellValue));
                    break;
                case "B":
                    user.setUserName(cellValue);
                    break;
                case "C":
                    user.setPhone(cellValue);
                    break;
                case "D":
                    try {
                        user.setHireDate(sdf.parse(cellValue));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    break;
                case "E":
                    user.setAddress(cellValue);
            }
        }
    }

    @Override
    public void endRow(int rowIndex) {  // 每一行的结束
        if(rowIndex != 0) {
            System.out.println(user);
        }
    }
}
